package com.eastfair.builder.config;//package com.eastfair.auth.config;
//
//import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
//import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
//import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS;
//import com.baomidou.mybatisplus.core.MybatisConfiguration;
//import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
//import com.baomidou.mybatisplus.core.config.GlobalConfig;
//import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
//import com.baomidou.mybatisplus.core.injector.ISqlInjector;
//import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
//import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
//import com.baomidou.mybatisplus.extension.plugins.inner.IllegalSQLInnerInterceptor;
//import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
//import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
//import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
//import com.eastfair.database.datasource.LampMetaObjectHandler;
//import com.eastfair.database.injector.LampSqlInjector;
//import com.eastfair.database.mybatis.WriteInterceptor;
//import com.eastfair.database.mybatis.typehandler.FullLikeTypeHandler;
//import com.eastfair.database.mybatis.typehandler.LeftLikeTypeHandler;
//import com.eastfair.database.mybatis.typehandler.RightLikeTypeHandler;
//import com.eastfair.database.properties.DatabaseProperties;
//import com.eastfair.databases.convert.CustomLocalDateTimeTypeHandler;
//import org.apache.ibatis.annotations.Mapper;
//import org.apache.ibatis.logging.Log;
//import org.apache.ibatis.logging.LogFactory;
//import org.apache.ibatis.mapping.DatabaseIdProvider;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.session.ExecutorType;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionTemplate;
//import org.mybatis.spring.mapper.ClassPathMapperScanner;
//import org.mybatis.spring.mapper.MapperFactoryBean;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.BeanFactory;
//import org.springframework.beans.factory.BeanFactoryAware;
//import org.springframework.beans.factory.ObjectProvider;
//import org.springframework.beans.factory.support.BeanDefinitionRegistry;
//import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ResourceLoaderAware;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
//import org.springframework.core.annotation.Order;
//import org.springframework.core.io.Resource;
//import org.springframework.core.io.ResourceLoader;
//import org.springframework.core.type.AnnotationMetadata;
//import org.springframework.util.Assert;
//import org.springframework.util.CollectionUtils;
//import org.springframework.util.ObjectUtils;
//import org.springframework.util.StringUtils;
//
//import javax.annotation.PostConstruct;
//import javax.sql.DataSource;
//import java.time.LocalDateTime;
//import java.util.Collections;
//import java.util.Iterator;
//import java.util.List;
//
//@Configuration
////@AutoConfigureAfter(MybatisPlusConfig.class)
//@EnableConfigurationProperties({MybatisPlusProperties.class})
//public class MybatisPlusAutoConfig {
//    private static final Log logger = LogFactory.getLog(MybatisPlusAutoConfig.class);
//    private  MybatisPlusProperties properties;
//    private  Interceptor[] interceptors;
//    private  ResourceLoader resourceLoader;
//    private  DatabaseIdProvider databaseIdProvider;
//    private  List<ConfigurationCustomizer> configurationCustomizers;
//    private  ApplicationContext applicationContext;
//
//    public MybatisPlusAutoConfig(MybatisPlusProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider, ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider, ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider, ApplicationContext applicationContext) {
//        this.properties = properties;
//        this.interceptors = (Interceptor[])interceptorsProvider.getIfAvailable();
//        this.resourceLoader = resourceLoader;
//        this.databaseIdProvider = (DatabaseIdProvider)databaseIdProvider.getIfAvailable();
//        this.configurationCustomizers = (List)configurationCustomizersProvider.getIfAvailable();
//        this.applicationContext = applicationContext;
//    }
//
//    @PostConstruct
//    public void checkConfigFileExists() {
//        if (this.properties.isCheckConfigLocation() && StringUtils.hasText(this.properties.getConfigLocation())) {
//            Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation());
//            Assert.state(resource.exists(), "Cannot find config location: " + resource + " (please add config file or check your Mybatis configuration)");
//        }
//    }
//
//    @Bean
//    @ConditionalOnMissingBean
//    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
//        MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
//        factory.setDataSource(dataSource);
//        factory.setVfs(SpringBootVFS.class);
//        if (StringUtils.hasText(this.properties.getConfigLocation())) {
//            factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
//        }
//
//        MybatisConfiguration configuration = this.properties.getConfiguration();
//        if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
//            configuration = new MybatisConfiguration();
//        }
//
//        if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
//            Iterator iterator = this.configurationCustomizers.iterator();
//
//            while(iterator.hasNext()) {
//                ConfigurationCustomizer customizer = (ConfigurationCustomizer)iterator.next();
//                customizer.customize(configuration);
//            }
//        }
//
//        if (null != configuration) {
//            configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
//        }
//
//        //时间转换
//        configuration.getTypeHandlerRegistry().register(LocalDateTime.class, new CustomLocalDateTimeTypeHandler());
//        factory.setConfiguration(configuration);
//        if (this.properties.getConfigurationProperties() != null) {
//            factory.setConfigurationProperties(this.properties.getConfigurationProperties());
//        }
//
//        if (!ObjectUtils.isEmpty(this.interceptors)) {
//            factory.setPlugins(this.interceptors);
//        }
//
//        if (this.databaseIdProvider != null) {
//            factory.setDatabaseIdProvider(this.databaseIdProvider);
//        }
//
//        if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
//            factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
//        }
//
//        if (StringUtils.hasLength(this.properties.getTypeEnumsPackage())) {
//            factory.setTypeEnumsPackage(this.properties.getTypeEnumsPackage());
//        }
//
//        if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
//            factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
//        }
//
//        if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
//            factory.setMapperLocations(this.properties.resolveMapperLocations());
//        }
//
//        GlobalConfig globalConfig;
//        if (!ObjectUtils.isEmpty(this.properties.getGlobalConfig())) {
//            globalConfig = this.properties.getGlobalConfig();
//        } else {
//            globalConfig = new GlobalConfig();
//        }
//
//        if (this.applicationContext.getBeanNamesForType(MetaObjectHandler.class, false, false).length > 0) {
//            MetaObjectHandler metaObjectHandler = (MetaObjectHandler)this.applicationContext.getBean(MetaObjectHandler.class);
//            globalConfig.setMetaObjectHandler(metaObjectHandler);
//        }
//
////        if (this.applicationContext.getBeanNamesForType(IKeyGenerator.class, false, false).length > 0) {
////            IKeyGenerator keyGenerator = (IKeyGenerator)this.applicationContext.getBean(IKeyGenerator.class);
////            globalConfig.setKeyGenerator(keyGenerator);
////        }
//
//        if (this.applicationContext.getBeanNamesForType(ISqlInjector.class, false, false).length > 0) {
//            ISqlInjector iSqlInjector = (ISqlInjector)this.applicationContext.getBean(ISqlInjector.class);
//            globalConfig.setSqlInjector(iSqlInjector);
//        }
//        factory.setGlobalConfig(globalConfig);
//        return factory.getObject();
//    }
//
//    @Bean
//    @ConditionalOnMissingBean
//    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
//        ExecutorType executorType = this.properties.getExecutorType();
//        return executorType != null ? new SqlSessionTemplate(sqlSessionFactory, executorType) : new SqlSessionTemplate(sqlSessionFactory);
//    }
////    @Bean
////    public GlobalConfig getGlobalConfig(){
////        GlobalConfig globalConfig = new GlobalConfig();
////        globalConfig.setSqlInjector( new DefaultSqlInjector() );
////        return new GlobalConfig();
////    }
//    @Configuration
//    @Import({AutoConfiguredMapperScannerRegistrar.class})
//    @ConditionalOnMissingBean({MapperFactoryBean.class})
//    public static class MapperScannerRegistrarNotFoundConfiguration {
//        public MapperScannerRegistrarNotFoundConfiguration() {
//        }
//
//        @PostConstruct
//        public void afterPropertiesSet() {
//            MybatisPlusAutoConfig.logger.debug("No " + MapperFactoryBean.class.getName() + " found.");
//        }
//    }
//
//    public static class AutoConfiguredMapperScannerRegistrar implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware {
//        private BeanFactory beanFactory;
//        private ResourceLoader resourceLoader;
//
//        public AutoConfiguredMapperScannerRegistrar() {
//        }
//
//        @Override
//        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
//            MybatisPlusAutoConfig.logger.debug("Searching for mappers annotated with @Mapper");
//            ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
//
//            try {
//                if (this.resourceLoader != null) {
//                    scanner.setResourceLoader(this.resourceLoader);
//                }
//
//                List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
//                if (MybatisPlusAutoConfig.logger.isDebugEnabled()) {
//                    Iterator iterator = packages.iterator();
//
//                    while(iterator.hasNext()) {
//                        String pkg = (String)iterator.next();
//                        MybatisPlusAutoConfig.logger.debug("Using auto-configuration base package '" + pkg + "'");
//                    }
//                }
//
//                scanner.setAnnotationClass(Mapper.class);
//                scanner.registerFilters();
//                scanner.doScan(StringUtils.toStringArray(packages));
//            } catch (IllegalStateException var7) {
//                MybatisPlusAutoConfig.logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled." + var7);
//            }
//
//        }
//
//        @Override
//        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
//            this.beanFactory = beanFactory;
//        }
//
//        @Override
//        public void setResourceLoader(ResourceLoader resourceLoader) {
//            this.resourceLoader = resourceLoader;
//        }
//    }
//
//
//    /**
//     * Mybatis Plus 注入器
//     *
//     * @return 注入器
//     */
//    @Bean("myMetaObjectHandler")
//    @ConditionalOnMissingBean
//    public MetaObjectHandler getMyMetaObjectHandler() {
//        return new LampMetaObjectHandler();
//    }
//
//
//
//
//
//
//    /**
//     * 演示环境权限拦截器
//     *
//     * @return 写入拦截器
//     */
//    @Bean
//    @Order(15)
//    @ConditionalOnMissingBean
//    @ConditionalOnProperty(prefix = DatabaseProperties.PREFIX, name = "isNotWrite", havingValue = "true")
//    public WriteInterceptor getWriteInterceptor() {
//        return new WriteInterceptor();
//    }
//
////
////    /**
////     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
////     * <p>
////     * 注意:
////     * 如果内部插件都是使用,需要注意顺序关系,建议使用如下顺序
////     * 多租户插件,动态表名插件
////     * 分页插件,乐观锁插件
////     * sql性能规范插件,防止全表更新与删除插件
////     * 总结: 对sql进行单次改造的优先放入,不对sql进行改造的最后放入
////     * <p>
////     * 参考：
////     * https://mybatis.plus/guide/interceptor.html#%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F-%E4%BB%A5%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB%B6%E4%B8%BE%E4%BE%8B
////     */
////    @Bean
////    @Order(5)
////    @ConditionalOnMissingBean
////    public MybatisPlusInterceptor mybatisPlusInterceptor() {
////        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
////        List<InnerInterceptor> beforeInnerInterceptor = getPaginationBeforeInnerInterceptor();
////        if (!beforeInnerInterceptor.isEmpty()) {
////            beforeInnerInterceptor.forEach(interceptor::addInnerInterceptor);
////        }
////
////        // 分页插件
////        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
////        // 单页分页条数限制
////        paginationInterceptor.setMaxLimit(databaseProperties.getLimit());
////        // 数据库类型
////        paginationInterceptor.setDbType(databaseProperties.getDbType());
////        // 溢出总页数后是否进行处理
////        paginationInterceptor.setOverflow(true);
////        interceptor.addInnerInterceptor(paginationInterceptor);
////
////
////        List<InnerInterceptor> afterInnerInterceptor = getPaginationAfterInnerInterceptor();
////        if (!afterInnerInterceptor.isEmpty()) {
////            afterInnerInterceptor.forEach(interceptor::addInnerInterceptor);
////        }
////
////        //防止全表更新与删除插件
////        if (databaseProperties.getIsBlockAttack()) {
////            interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
////        }
////        // sql性能规范插件
////        if (databaseProperties.getIsIllegalSql()) {
////            interceptor.addInnerInterceptor(new IllegalSQLInnerInterceptor());
////        }
////
////        return interceptor;
////    }
//
//
//    /**
//     * 分页拦截器之后的插件
//     *
//     * @return 分页拦截器之后的插件
//     */
//    protected List<InnerInterceptor> getPaginationAfterInnerInterceptor() {
//        return Collections.emptyList();
//    }
//
//    /**
//     * 分页拦截器之前的插件
//     *
//     * @return 分页拦截器之前的插件
//     */
//    protected List<InnerInterceptor> getPaginationBeforeInnerInterceptor() {
//        return Collections.emptyList();
//    }
//
//    /**
//     * mybatis-plus 3.4.0开始采用新的分页插件,一缓和二缓遵循mybatis的规则,
//     * 需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
//     * (该属性会在旧插件移除后一同移除)
//     */
//    @Bean
//    public ConfigurationCustomizer configurationCustomizer() {
//        return configuration -> configuration.setUseDeprecatedExecutor(false);
//    }
//
////    /**
////     * Mybatis Plus 注入器
////     *
////     * @return 注入器
////     */
////    @Bean("myMetaObjectHandler")
////    @ConditionalOnMissingBean
////    public MetaObjectHandler getMyMetaObjectHandler() {
////        return new LampMetaObjectHandler();
////    }
//
//
//    /**
//     * Mybatis 自定义的类型处理器： 处理XML中  #{name,typeHandler=leftLike} 类型的参数
//     * 用于左模糊查询时使用
//     * <p>
//     * eg：
//     * and name like #{name,typeHandler=leftLike}
//     *
//     * @return 左模糊处理器
//     */
//    @Bean
//    public LeftLikeTypeHandler getLeftLikeTypeHandler() {
//        return new LeftLikeTypeHandler();
//    }
//
//    /**
//     * Mybatis 自定义的类型处理器： 处理XML中  #{name,typeHandler=rightLike} 类型的参数
//     * 用于右模糊查询时使用
//     * <p>
//     * eg：
//     * and name like #{name,typeHandler=rightLike}
//     *
//     * @return 右模糊处理器
//     */
//    @Bean
//    public RightLikeTypeHandler getRightLikeTypeHandler() {
//        return new RightLikeTypeHandler();
//    }
//
//    /**
//     * Mybatis 自定义的类型处理器： 处理XML中  #{name,typeHandler=fullLike} 类型的参数
//     * 用于全模糊查询时使用
//     * <p>
//     * eg：
//     * and name like #{name,typeHandler=fullLike}
//     *
//     * @return 全模糊处理器
//     */
//    @Bean
//    public FullLikeTypeHandler getFullLikeTypeHandler() {
//        return new FullLikeTypeHandler();
//    }
//
//
//    @Bean
//    @ConditionalOnMissingBean
//    public LampSqlInjector getMySqlInjector() {
//        return new LampSqlInjector();
//    }
//
//}
